<template>
    <div class="address_detail">
        <van-address-edit
        :address-info='addressInfo'
        :area-list="areaList"
        show-set-default
        show-search-result
        :show-delete='showDelete'
        :area-columns-placeholder="['请选择', '请选择', '请选择']"
        @save="onSave"
        @delete="onDelete"
        />
    </div>
</template>
<script>
import { mapState } from 'vuex'
import areaList from '../../assets/js/area.js'

export default {
    data(){
        return{
            areaList,
            addressInfo:{},
            showDelete:false
        }
    },
    computed:{
        ...mapState(['openid']),
        api() {
            return this.$route.params.id == 'none' ? this.$api.addressincrease : this.$api.addressmodify
        }
    },
    mounted(){
        if (this.$route.params.id !='none') {
            this.get()
            
        }
    },
    methods:{
        //获取详情
        async get(){
            let form={
                id: this.$route.params.id,
                openid: this.openid
            }
            let { data: { code, data} } = await this.$http.get(this.$api.addressshow,form)
            if (code === 0) {
                this.addressInfo={
                    id:data.id,
                    name:data.name,
                    tel:data.mobile,
                    city:data.city,//城市
                    province:data.province,//省份
                    addressDetail:data.address,//详细地址
                    county:data.district,//区县
                    isDefault:data.is_default ==0?true:false,
                    areaCode:String(data.num)
                }
                this.showDelete = true
            }
        },
        //删除
        async onDelete(){

            console.log('删除')
            let {data:{code,data}} = await this.$http.post(this.$api.addressExcept,{openid:this.openid,id:this.addressInfo.id})
            if (code === 0) {
                this.$router.push('/address')
            }

        },
        //保存
        async onSave(content) {
            console.log(content)
            let form={
                name: content.name,
                num:content.areaCode,
                mobile: content.tel,
                province: content.province,
                city: content.city,
                district: content.county,
                address: content.addressDetail,
                id: this.$route.params.id !== 'none' ? this.$route.params.id : '',
                openid: this.openid
            }
            
            let {data:{code,data}} = await this.$http.post(this.api,form)
            if (code === 0) {
                this.modifyDefault()
                
            }

        },
        async modifyDefault(){
            let form = {
                id:this.addressInfo.id,
                openid:this.openid
            }
            let{data:{code,data}} = await this.$http.post(this.$api.addressdefault,form)
            if (code === 0) {
                this.$router.push('/address')
            }
        }
    }
}
</script>
<style lang="scss" scoped>

</style>